package meijuli.com.join51.business.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Switch;
import android.widget.TextView;

import meijuli.com.join51.business.R;
import meijuli.com.join51.business.model.Category;

import java.util.List;

/**
 * 分类页面适配器
 *
 * @author wenqin 2017-04-25 14:34
 */

public class ClassificationAdapter extends RecyclerView.Adapter<ClassificationAdapter.VH> {

    private LayoutInflater mInflater;
    private List<Category> data;
    private OnEditClickListener mOnEditClickListener;
    private OnItemClickListener mOnItemClickListener;
    private OnCheckChangedListener mOnCheckChangedListener;

    public ClassificationAdapter(Context context, List<Category> data) {
        this.data = data;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
        return new VH(mInflater.inflate(R.layout.adapter_classification, parent, false));
    }

    @Override
    public void onBindViewHolder(VH holder, int position) {
        Category category = data.get(position);

        if (category.getStatus() == Category.STATUS_CLOSE) {
            holder.mStatusSwitch.setChecked(false);
            holder.mClassificationText.setBackgroundResource(R.drawable.category_close);
        } else {
            holder.mStatusSwitch.setChecked(true);
            holder.mClassificationText.setBackgroundResource(R.drawable.category_normal);
        }

        holder.mClassificationText.setText(category.getCategoryName());
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    class VH extends RecyclerView.ViewHolder {
        TextView mClassificationText;
        TextView mEditText;
        Switch mStatusSwitch;

        VH(View itemView) {
            super(itemView);
            mClassificationText = itemView.findViewById(R.id.classification_text);
            mEditText = itemView.findViewById(R.id.edit_text);
            mStatusSwitch = itemView.findViewById(R.id.status_switch);
            mEditText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnEditClickListener != null) {
                        mOnEditClickListener.editClick(getAdapterPosition(), mStatusSwitch.isChecked());
                    }
                }
            });

            mStatusSwitch.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnCheckChangedListener != null) {
                        mOnCheckChangedListener.onCheckedChanged(mStatusSwitch.isChecked(),
                                getAdapterPosition());
                    }
                }
            });

            // disable switch drag
            mStatusSwitch.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    return event.getActionMasked() == MotionEvent.ACTION_MOVE;
                }
            });

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(view, getAdapterPosition());
                    }
                }
            });
        }
    }

    public interface OnEditClickListener {
        void editClick(int position, boolean isCheck);
    }

    public interface OnCheckChangedListener {
        void onCheckedChanged(boolean isCheck, int position);
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnEditClickListener(OnEditClickListener onEditClickListener) {
        mOnEditClickListener = onEditClickListener;
    }

    public void setOnCheckChangedListener(OnCheckChangedListener onCheckChangedListener) {
        mOnCheckChangedListener = onCheckChangedListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }
}
